<?php

namespace App\Http\Controllers\Api;

use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\SystemRegion;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class SystemController extends Controller
{

    //  接口请求
    protected $apiRequest = '';

    //  TOKEN
    protected $token = '';

    //  TOKEN中用户信息
    protected $tokenUser = '';


    public function __construct(Request $request)
    {
        $this->apiRequest = $request;

        if (auth()->user()) {

            $this->tokenUser = auth()->user();

        }

    }


    /**
     * 设置位置信息
     * @return void
     */
    public function regionSet()
    {
        $region_id = intval($this->apiRequest->input('region_id', 0));

        $region_info = SystemRegion::find($region_id);

        if (empty($region_info) || !isset($region_info)) {

            return jsonError('抱歉，位置信息异常。');

        } else {

            Redis::set('user:region:id', $region_id);

        }
    }


    /*
     * 用户协议1 隐私协议2 联系我们3 使用许可4 隐私申明5 积分规则6 关于我们7
     * @params int $type
     * @return array
     */
    public function protocol(Request $request)
    {
        $type        = $request -> input('type');
        $articleType = 1;
        $categoryId  = 33;
        $article_ids = [1 => '43', 2 => '44', 3 => '45', 4 => '46', 5 => '47', 6 => '48', 7 => '52'];

        if(!in_array($type,array_keys($article_ids))){
            return jsonSuccess();
        }

        $protocol = Article::where('types',$articleType);

        if($type){
            $protocol ->where('id', $article_ids[$type]);
        }else{
            $protocol ->whereIn('category_id', ArticleCategory::getChild($categoryId));
        }

        $protocol = $protocol -> first();

        return jsonSuccess($protocol);
    }


    /*地区列表*/
    public function systemRegion(Request $request)
    {
        $region_id   = $request -> input('region_id');
        $region_code = $request -> input('region_code');


        $systemRegion = SystemRegion::select(['id','region_name','region_code']);
        if($region_id){
            $systemRegion -> where('id',$region_id);
        }
        if($region_code){
            $systemRegion -> where('region_code',$region_code);
        }

        $systemRegion = $systemRegion -> where('status','1') -> get();


        return jsonSuccess($systemRegion);
    }
}
